package com.example.boot.controller;

import com.example.boot.bean.*;

import com.example.boot.bean.pojo.CountDetail;

import com.example.boot.bean.pojo.ReplyDetail;
import com.example.boot.service.admin.AdminService;
import com.example.boot.service.user.UserService;
import com.example.boot.utils.RedisLock;
import com.example.boot.utils.Result;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
public class MyController {
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisLock redisLock;


    @PostMapping("/login/user")
    public Result userLogin(@Validated  User user) {
        User user1 = userService.userLogin(user);
        if (user1!=null) {
            request.getSession().setAttribute("user",user1);
            return Result.success().add("用户登录信息", "用户登录成功");
        }
        return Result.fail().add("登录信息", "登录失败");
    }
    @PostMapping("/login/admin")
    public Result userLogin( Admin admin) {
        Admin admin1 = adminService.adminLogin(admin);
        if (admin1!=null) {
            request.getSession().setAttribute("admin",admin1);
            return Result.success().add("客服登录信息", "客服登录成功");
        }
        return Result.fail().add("登录信息", "登录失败");
    }

    //用户提问
    @PostMapping("/save/{id}")
    public Result saveque(@RequestBody ReplyInfo replyInfo,@PathVariable("id") int id) {
        userService.save(replyInfo,id);

        return Result.success().add("提交", "问题提交成功");
    }

   /* //左侧问题列表
    @GetMapping("/PageList/{pageNum}")
    public Result listQuestion(@PathVariable("pageNum") Integer pageNum) {
        List<CountDetail> list = adminService.queryList(pageNum);

        return Result.success().add("查询信息", list);
    }*/
    //左侧问题列表
    @GetMapping("/PageList/left/{Num}")
    public Result listQuestion(@PathVariable("Num") int num ) {
        List<CountDetail> list = adminService.queryList(num);
        if (list==null){
            return Result.success().add("处理完毕","没有数据");
        }else

        return Result.success().add("查询信息", list);
    }



    //问题拉取
    /**
     * 超时时间 5s
     */
    private static final int TIMEOUT = 5*1000;
    @GetMapping("/PageList/pull")
    public Result pull(){
        //上锁
        long time = System.currentTimeMillis() + TIMEOUT;
        if(!redisLock.lock("key", String.valueOf(time))){
            return Result.success().add("处理完毕","排队人数太多，请稍后再试.");
        }
        System.out.println("获得锁的时间戳："+String.valueOf(time));
        try {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        int aid = admin.getId();
        List pull = adminService.pull(aid);
        if (pull!=null){
        return Result.success().add("处理完毕",pull);
     }else
         return Result.success().add("处理完毕","暂无数据");
        }finally {
            //释放锁
            redisLock.release("key", String.valueOf(time));
            System.out.println("释放锁的时间戳："+String.valueOf(time));
        }
    }



    //右侧问题列表,传递用户id
    @GetMapping("/PageList/{id}/{pageNum}")
    public Result listQuestion2(@PathVariable("id") int uid ,@PathVariable("pageNum") Integer pageNum) {
        List<ReplyDetail> list = adminService.queryList2(uid, pageNum);
        return Result.success().add("查询信息", list);
    }


    //回复
    @PostMapping("/admin/answer/{id}")
    public Result answer(@PathVariable("id") int id,String answer ) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        int id1 = admin.getId();
        adminService.alter(id,answer,id1);
        return Result.success().add("修改信息", "回复成功");
    }
    //关闭问题
    @GetMapping("/admin/answer/close/{id}")
    public Result close(@PathVariable("id") int id){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        String adminName = admin.getAdminName();
        int aid = admin.getId();
        StringBuffer close = adminService.close(adminName,id,aid);
        return Result.success().add("关闭消息",close
        );

    }

    //添加设置常见问题
    @PostMapping("/admin/add")
    public Result add(@RequestBody Question question){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        int id = admin.getId();
        adminService.add(question,id);
        return Result.success().add("添加问题","添加成功");


    }
    //将自定义问题加序号排列展示
    @GetMapping("/admin/ListByOrder")
    public Result ListByOrder(){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        int id = admin.getId();
        List<StringBuffer> questions = adminService.ListByOrder(id);
        return Result.success().add("问题列表",questions);

    }

    //删除自定义问题
    @PostMapping("/admin/delete/{id}")
    public Result delete(@PathVariable("id") int id){
        adminService.delete(id);
        return Result.success().add("自定义回复","删除成功");

    }


}
